<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="isperp" uri="/WEB-INF/tlds/isperp.tld"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<c:choose>
	<c:when test="${coseWithAccounting eq true }">
		<style type="text/css">
<!--
.cost_table {
	width: 90%;
	margin: 0 auto;
	padding: 5px;
	border-collapse: collapse;
	border-spacing: 0;
	border: 1px solid #999;
}

.cost_table th {
	
}

.cost_table td,.cost_table th {
	border: 1px solid #999;
	padding: 2px 3px;
}

.olditem,.iscalculate {
	border: none !important;
	box-shadow: none !important;
}

.iscalculate,.isnotcalculate {
	
}
-->
</style>


		<table id="cost_table" class="cost_table">${projectCost }
		</table>
		<isperp:buttonList>
			<button class="submit nui-mainbtn" onclick="save();">保存</button>
		</isperp:buttonList>

		<script type="text/javascript">
			function closeWin() {
				if (isperp.getUrlParamByName("pcontrolid") != undefined)
					isperp.reloadParentControl(isperp.getUrlParamByName("pcontrolid"));
				isperp.closeDialog({
					id : '',
					empty : true
				});
			}

			function save() {
				var tdata = "{";
				var projectId = '${projectId}';
				$("[id^='newcost_']").each(function(index, item) {
					var $item = $(item);
					var v = $item.val();
					var vid = $item.data("code");
					tdata += "'" + vid + "':" + "'" + v + "',";
				});
				tdata = tdata.substring(0, tdata.length - 1);
				tdata = tdata + "}";
				$.ajax({
					url : 'project/costAccountingEntry/saveCostAccount',
					data : {
						"tdata" : tdata,
						"projectId" : projectId
					},
					success : function(data) {
						closeWin();
					}
				});
			}

			$("#cost_table tr:first").find("th:last").after("<th>实际费用</th>");

			$.post("project/project/getCostAccountingEntryAmount?projectId=${projectId}", function(data) {

				$("#cost_table tr").find("td:last").each(function(index, item) {
					var $item = $(item);
					var $input = $item.find("input:first");
					var cls = $input.attr("class");
					var html = "<td class='newtd'>"

					var isfinded = false;
					var result = undefined;

					var code = $input.data("code");
					$(data).each(function(index, item) {
						if (code == item.code) {
							isfinded = true;
							result = item;
							return;
						}
					});

					if (isfinded) {

						if (result.amount_c > $input.val()) {
							html += "<input style='color:red' class='isnotcalculate' data-code='" + $input.data("code") + "' data-calculate='" + $input.data("calculate") + "' id='newcost_" + $input.data("code") + "' value='";
						} else {
							html += "<input class='isnotcalculate' data-code='" + $input.data("code") + "' data-calculate='" + $input.data("calculate") + "' id='newcost_" + $input.data("code") + "' value='";
						}

						html += result.amount_c + "'></td>";

					} else {
						html += "<input class='isnotcalculate' data-code='" + $input.data("code") + "' data-calculate='" + $input.data("calculate") + "' id='newcost_" + $input.data("code") + "' value='0'></td>";

					}

					$input.removeClass("isnotcalculate");

					$input.addClass("olditem");
					$item.after(html);

					$("input [id^='newcost']").change(function() {
						cc();
					});

				});

				var SUM = "${account}";

				var cc = function cc() {
					var e_js = "";
					//从上到下，从左到右
					//找到所有手工填写的
					$(".isnotcalculate").each(function(index, item) {
						var $item = $(item);
						var d_code = $item.data("code");

						var i_v = $item.val();
						if (!i_v || i_v == "") {

							i_v = 0;
						}

						e_js += "var " + d_code + "=" + i_v + ";";
					});

					//组合所有的js
					$(".iscalculate").each(function(index, item) {
						var $item = $(item);
						var d_code = $item.data("code");
						var d_calculate = $item.data("calculate").replace(new RegExp("%", "gm"), "/100");

						e_js += "var " + d_code + "=" + d_calculate + ";";

					});

					//组合所有的js
					$(".iscalculate").each(function(index, item) {
						var $item = $(item);
						var d_code = $item.data("code");
						e_js += "$('#newcost_" + d_code + "').val(" + d_code + ");";
					});
					eval(e_js);
				};

				cc();

				//$(".isnotcalculate").addClass("iscalculate");

			});
		</script>
	</c:when>
	<c:otherwise>
${projectCost }
</c:otherwise>

</c:choose>

